package com.shycloud.mido.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.member.api.entity.MultiClassroom;
import com.shycloud.mido.member.api.entity.MultiStudentClassroomLink;
import com.shycloud.mido.member.api.entity.MultiTeacherClassroomLink;
import com.shycloud.mido.member.api.vo.AppMultiStudentClassroomVO;
import com.shycloud.mido.member.api.vo.MultiClassRoomPageVO;
import com.shycloud.mido.member.api.vo.MultiClassroomVO;
import com.shycloud.mido.member.api.vo.MyTeacherVO;
import com.shycloud.mido.member.api.vo.StudentVO;
import com.shycloud.mido.member.mapper.MultiClassroomMapper;
import com.shycloud.mido.member.mapper.MultiStudentClassroomLinkMapper;
import com.shycloud.mido.member.mapper.MultiTeacherClassroomLinkMapper;
import com.shycloud.mido.member.service.MultiClassroomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.member.service.OrgTeacherDetailedService;
import com.shycloud.mido.member.service.TeacherService;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2021-07-12
 */
@Service
@AllArgsConstructor
public class MultiClassroomServiceImpl extends ServiceImpl<MultiClassroomMapper, MultiClassroom> implements MultiClassroomService {

	/** 教室内老师业务 mapper */
	private final MultiTeacherClassroomLinkMapper teacherClassroomLinkMapper;
	/** 教室内学生业务 mapper */
	private final MultiStudentClassroomLinkMapper studentClassroomLinkMapper;
	/** 个人教师模块 service */
	private final TeacherService teacherService;

	@Override
	public List<MultiClassroomVO> getStudentList(String teacherId) {

		//获取此教师管理的班级列表
		List<MultiClassroomVO> multiClassroomVOS = teacherClassroomLinkMapper.getClassRoomListByTeacherId(teacherId);
		//循环班级获取学生列表
		for (MultiClassroomVO multiClassroomVO : multiClassroomVOS) {
			List<StudentVO> students = studentClassroomLinkMapper.getClassroomStudent(multiClassroomVO.getId(), teacherId, null);
			multiClassroomVO.setStudentDetaileds(students);
		}

		return multiClassroomVOS;
	}

	@Override
	public IPage<MultiClassRoomPageVO> getMultiClassRoomPage(Page page, Integer orgId, String name) {
		return baseMapper.getMultiClassRoomPage(page, orgId, name);
	}

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 3600)
	public Boolean delMultiClassRoom(MultiClassroom classroom) {

		if (classroom == null || classroom.getId() == null) {
			throw new BusinessException("所传参数不正确");
		}

		//只有空教室才可删除
		Integer teaCount = teacherClassroomLinkMapper
				.selectCount(new QueryWrapper<MultiTeacherClassroomLink>().eq("classroom_id", classroom.getId()));

		Integer stuCount = studentClassroomLinkMapper
				.selectCount(new QueryWrapper<MultiStudentClassroomLink>().eq("classroom_id", classroom.getId()));

		if (teaCount == 0 && stuCount == 0) {
			//执行删除逻辑
			baseMapper.deleteById(classroom.getId());
		} else {
			throw new BusinessException("请先将教师内成员全部移除");
		}

		return true;
	}

	@Override
	public List<AppMultiStudentClassroomVO> getClassRoomDetail(String teacherId, String studentId) {

		List<AppMultiStudentClassroomVO> resList = new ArrayList<>();

		List<MultiClassroomVO> multiClassrooms = new ArrayList<>();

		//获取此教师下班级列表
		if (StringUtils.isNotBlank(teacherId)) {
			multiClassrooms = teacherClassroomLinkMapper.getClassRoomListByTeacherId(teacherId);
		}

		if (StringUtils.isNotBlank(studentId)) {
			multiClassrooms = studentClassroomLinkMapper.getClassRoomListByStudentId(studentId);
		}

		for (MultiClassroom multiClassroom : multiClassrooms) {

			AppMultiStudentClassroomVO appMultiStudentClassroomVO = new AppMultiStudentClassroomVO();
			//班级ID
			appMultiStudentClassroomVO.setClassroomId(multiClassroom.getId());
			//班级名称
			appMultiStudentClassroomVO.setClassOrGroupName(multiClassroom.getName());
			//范围
			appMultiStudentClassroomVO.setScope(multiClassroom.getScope());
			//获取学生列表
			List<StudentVO> classroomStudent = studentClassroomLinkMapper.getClassroomStudent(multiClassroom.getId(), teacherId, null);
			//学生数量
			appMultiStudentClassroomVO.setStudentCount(classroomStudent.size());
			appMultiStudentClassroomVO.setStudents(classroomStudent);

			//获取教师列表
			List<MyTeacherVO> teacherList = teacherClassroomLinkMapper.getTeacherListByRoomId(multiClassroom.getId(), null);
			//教师数量
			appMultiStudentClassroomVO.setTeacherCount(teacherList.size());
			appMultiStudentClassroomVO.setTeacherS(teacherList);

			resList.add(appMultiStudentClassroomVO);

		}
		return resList;
	}

	@Override
	public String getClassRoomId(String teacherId, String studentId) {

		//1.先判断是否是个人教师关系
		MyTeacherVO myTeacher = teacherService.getMyTeacher(studentId);

		if (myTeacher != null) {
			if (StringUtils.equals(teacherId, myTeacher.getTeacherId())) {
				return null;
			}
		}

		//获取班级信息
		List<MultiClassroom> classrooms = baseMapper.getClassRoomByStuAndTea(teacherId, studentId);

		if (CollectionUtils.isEmpty(classrooms)) {
			return null;
		} else {
			return classrooms.get(0).getId();
		}

	}
}
